package org.codeasy.ceutils.string;

import java.util.ArrayList;
import java.util.List;

import org.codeasy.ceutils.exception.StringRTException;
import org.codeasy.ceutils.regexp.RegExpConstant;
import org.codeasy.ceutils.set.ArrayUtil;

public class StringUtil {

	/**
	 * 判断是否为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isBlank(String str) {
		if (str == null || "".equals((StringUtil.removeBlank(str.trim())))) {
			return true;
		}
		return false;
	}

	/**
	 * 把大于等于两个空格或制表符的替换为只保留一个空格
	 * 
	 * @param str
	 * @return
	 */
	public static String blankMany2One(String str) {
		return str.replaceAll(RegExpConstant.TWO_MORE_BLANK, " ");
	}

	/**
	 * 移除所有空格或制表符
	 * 
	 * @param str
	 * @return
	 */
	public static String removeBlank(String str) {
		return str.replaceAll(RegExpConstant.ONE_BLANK, "");
	}

	/**
	 * 判断是否存在为空的字符串
	 * 
	 * @param strs
	 * @return
	 */
	public static boolean isBlank(String... strs) {
		for (int i = 0; i < strs.length; i++) {
			if (StringUtil.isBlank(strs[i])) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 根据开始符号和结束符号获取符号之间的内容
	 * Demo:
	 * getBetweenContent("fds[0009999]fs}}]dfds", "[", "]");
	 * 
	 * OutPut:0009999]fs}}
	 * 
	 * @param str 原始字符串
	 * @param startFlag 从开始取
	 * @param endFlag 从最后开始取，多个时取最后一个
	 * @return
	 */
	public static String getBetweenContent(String str, String startFlag, String endFlag) {
		if (StringUtil.isBlank(str, startFlag, endFlag)) {
			return null;
		}
        if (str.indexOf(startFlag) != -1) {
        	str = str.substring(str.indexOf(startFlag)+1);
        } 
        if (str.lastIndexOf(endFlag) != -1) {
        	str = str.substring(0, str.lastIndexOf(endFlag));
        }
		return str;

	}
	
	/**
	 * 获取以某个字符开始的字符串
	 * Demo:
	 * 	List<String> list = new ArrayList<String>();
	 *	list.add("SELECT");
	 *	list.add("DELETE");
	 *	System.out.println(getStartFlagStr("This is SQL: Select * from table ;",list));
	 * 
	 * OutPut: Select * from table ;
	 * 
	 * @param str
	 * @param startWithList
	 * @return
	 */
	public static String getStartFlagStr(String str, List<String> startWithList) {
		for (int i = 0; i < startWithList.size(); i++) {
			int indexOf = str.toLowerCase().indexOf(startWithList.get(i).toLowerCase());
			if (indexOf != -1) {
				return str.substring(indexOf);
			}
		}
		return null;
	}
	
	/**
     * 修改首字母为大写
     * 如：把abc_def_ghi_jkl为AbcDefGhiJkl
     * @param str
     * @param split
     * @return
     */
    public static String firstUpperCase(String str, String split) {
        if (str == null) {
            return null;
        }
        if (split != null) {
            String[] strArray = str.split(split);
            StringBuffer sb = new StringBuffer();
            for (int i = 0 ; i < strArray.length; i++) {
                sb.append(firstUpperCase(strArray[i]));
            }
            return sb.toString();
        } else {
            return firstUpperCase(str);
        }
    }
    
    /**
     * 将首字母改为大写
     * 
     * @param str
     * @return
     */
    public static String firstUpperCase(String str) {
        if (str == null) {
            return null;
        }
        String firstLetter = str.substring(0,1);
        if (isLetter(firstLetter)) {
            return firstLetter.toUpperCase() + str.substring(1);
        } else {
            return str;
        }
        
    }
    
    /**
     * 将首字母改为小写
     * @param str
     * @return
     */
    public static String firstLowerCase(String str) {
        if (str == null) {
            return null;
        }
        String firstLetter = str.substring(0,1);
        if (isLetter(firstLetter)) {
            return firstLetter.toLowerCase() + str.substring(1);
        } else {
            return str;
        }
    }
    
    /**
     * 判断是否是字母
     * 
     * @param str
     * @return
     */
    public static boolean isLetter(String str) {
        if (str == null) {
            return false;
        } 
        return str.matches("[a-zA-Z]+");
    }
    
    /**
     * 把空转为空串
     * 
     * @param str
     * @return
     */
    public static String null2Blank(String str) {
        return str == null ? "" : str;
    }
    
    /**
     * 把字符串转为Long
     * @param value
     * @return
     */
    public static Long longValueOf(String value) {
        if (value != null) {
            try {
                Long.valueOf(value);
            } catch (Exception e) {
                return null;
            }
        }
        return null;
    }
    
    /**
     * 移除最后一个字符
     * 
     * @param str
     * @return
     * @throws Exception 
     */
    public static String removeLastStr(String str, int remCount) {
    	if (StringUtil.isBlank(str)) {
    		return str;
    	}
    	if (str.length() < remCount) {
    		throw new StringRTException("ERROR: remCount can not more than string length");
    	}
    	return str.substring(0, str.length() - remCount);
    }
    
    /**
     * 获取某个字符之前的所有字符串
     * Demo:getBeforeStr("fdsfdsfds00099", "9")
     * outPut: fdsfdsfds000
     * @param str
     * @param flag
     * @return
     */
    public static String getBeforeStr(String str, String flag) {
    	if (StringUtil.isBlank(str,flag)) {
    		return str;
    	} 
    	int pos = str.indexOf(flag);
    	return pos != -1 ? str.substring(0,str.indexOf(flag)) : str;
    }
    
    /**
     * 获取某个字符之后的所有字符串
     * Demo:getBeforeStr("fdsfdsfds00099", "9")
     * outPut: fdsfdsfds000
     * @param str
     * @param flag
     * @return
     */
    public static String getAfterStr(String str, String flag) {
    	if (StringUtil.isBlank(str,flag)) {
    		return str;
    	} 
    	int pos = str.indexOf(flag);
    	return pos != -1 ? str.substring(pos + flag.length()) : str;
    }
    
    /**
     * 
     * 查找字符str中含有findStr字符串的个数
     * 
     * @param str
     * @param findStr
     * @return
     */
    public static int containCount(String str, String findStr) {
    	int i = 0;
    	while (str.indexOf(findStr) != -1) {
    		str = str.substring(str.indexOf(findStr)+1);
    		i++;
    	}
    	return i;
    }
    
    /**
     * 移除字符串的某些前缀
     * 
     * @param str 源字符串
     * @param prefixStr 前缀，多个以逗号分隔
     * @param ignoseCase 是否忽略大小写， true -忽略大小写， false - 不忽略
     * @return
     */
    public static String removePrefix(String str, String prefixStr, boolean ignoseCase) {
    	if (StringUtil.isBlank(str, prefixStr)) {
    		return str;
    	}
    	
    	if (ignoseCase) {
    		str = str.toLowerCase();
    		prefixStr = prefixStr.toLowerCase();
    	}
    	
    	String[] prefixArray = prefixStr.split(",");
        for (int i = 0; i < prefixArray.length; i++) {
        	if (str.startsWith(prefixArray[i])) {
        		return str.substring(prefixArray[i].length());
        	}
        }
        return str;
    }
    
 
	
	public static void main(String[] args) {
	}

}
